package labox.innovation.loginserver.socket.clientpackets;

import labox.innovation.loginserver.LoginController;
import labox.innovation.loginserver.socket.serverpackets.ResponseChangePassWord;
import labox.innovation.loginserver.socket.serverpackets.LoginFail.LoginFailReason;

/**
 * 接收修改密码请求
 * 
 * @author Frank
 * 
 */
public final class RequestChangePassWord extends LoginClientPacket {
	/** 用户名 */
	private String _userName;

	/** 密码 */
	private String _passWord;

	/***/
	private String _newPassWord;

	@Override
	protected void readImpl() {
		_userName = readString();
		_passWord = readString();
		_newPassWord = readString();
	}

	@Override
	public void run() {
		LoginController lc = LoginController.getInstance();
		//进行必要的简单检查
		if (_userName == null || !lc.isValidName(_userName)) {
			getClient().close(new ResponseChangePassWord(ResponseChangePassWord.RESPONSE_ACCOUNTORPASSWORD_ERROR, null));
			return;
		}

		if (_passWord == null || !lc.isValidPassword(_passWord)) {
			getClient().close(new ResponseChangePassWord(ResponseChangePassWord.RESPONSE_ACCOUNTORPASSWORD_ERROR, null));
			return;
		}

		if (_newPassWord == null || !lc.isValidPassword(_newPassWord)) {
			getClient().close(new ResponseChangePassWord(ResponseChangePassWord.RESPONSE_NEWPASSWORD_ERROR, null));
			return;
		}

		byte responseCode = LoginController.getInstance().resetPassWord(_userName, _passWord, _newPassWord);
		getClient().sendPacket(new ResponseChangePassWord(responseCode, _newPassWord));
	}
}
